import CommonAction from './common';
import React from 'react';
import mock from '../mock'; //排序自定义数据
import language from '../mock/language';
import {getAppPageConfig} from '../../../../hrpub/common/utils/utils';

export default class MainAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        const {props} = this.comp;
        if (props.use) {
            props.use.table('sort');
            props.use.form('uniqueTemp', 'bd_psndoc', 'batch_edit', 'stopPoi', 'passaway', 'batchAddSub', 'hi_psnorg_h', 'hi_psnjob_h');
            props.use.editTable('poi_list');
            props.use.search('query');
            // props.use.cardTable("hi_psnjob", "hi_psndoc_cert");
        }
    }

    appConfig = getAppPageConfig();
    // 获取多语
    getLanguage = () => {
        const {props} = this.comp;
        const {
            dispatch,
            MultiInit
        } = props;

        MultiInit.getMultiLang({
            moduleId: 'hi6007',
            domainName: 'hrhi',
            callback: (json, status, init) => {
                let languageJson = Object.assign(language, json);
                dispatch({
                    type: 'main/update',
                    payload: {
                        language: languageJson
                    }
                });
                this.getTemplate();
            }
        });
    };

    // 获取模版信息
    getTemplate = () => {
        const {props} = this.comp;
        const {
            meta,
            button,
            dispatch,
            createUIDom
        } = props;

        createUIDom(this.appConfig, (res) => {
            Object.assign(res.template, mock().template);
            let subTables = []
            Object.keys(res.template.gridrelation).forEach(item => {
                if(item !== 'poi_list' && item !== 'sort') {
                    subTables.push(item)
                }
            })
            // props.use && props.use.editTable(...subTables)
            props.use && props.use.cardTable(...subTables)
            meta.setMeta(this.processTemplate(res.template));
            button.setButtons(res.button);
            dispatch({
                type: 'main/update',
                payload: {
                    context: res.context,
                    appConfig: this.appConfig,
                    oid: res.template.query.oid,
                    subTables: subTables
                }
            });
        });
    };

    // 处理模版数据
    processTemplate = (template) => {
        const {props: {main: {language}}, action} = this.comp;
        template['poi_list'].items.push({
            attrcode: 'handleArea',
            itemtype: 'customer',
            hyperlinkflag: false,
            label: language['hi6007-000029'], /* 国际化处理： 操作*/
            width: '180px',
            textAlign: 'center',
            visible: true,
            fixed: 'right',
            render(text, record, index) {
                return (
                    <div>
                        <span style={{color: '#0073E1', cursor: 'pointer', marginRight: '12px'}}
                              onClick={() => action.tableAct.editPersonnelInfo(record)}
                        >
                            {language['hi6007-000030']}
                            {/* 修改 */}
                        </span>
                        <span style={{color: '#0073E1', cursor: 'pointer', marginRight: '12px'}}
                              onClick={() => action.tableAct.deletePersonnelInfo(record, index)}
                        >
                            {language['hi6007-000031']}
                            {/* 删除 */}
                        </span>
                    </div>
                )
            }
        });
        return template
    };

    // 初始化
    didMount = () => {
        this.getLanguage();
        // 设置左树初始值
        // this.comp.action.treeAct.customTree();
        // 设置按钮状态
        this.comp.action.btnAct.noneOrgButtonStatus();
        // 初始化高度设置
        // this.comp.action.headerAct.getPageHeight();
    };

    // 卸载
    willUnMount = () => {
        // this.pubSub.unSubscribe('getMainTableData');
    };

    // 事件订阅部分
    subscribe = () => {
        const {comp: {action}, pubSub} = this;
        // pubSub.subscribe('getMainTableData', (...args) => {
        //     action.tableAct.getMainTableData(...args);
        // });
    };

}